/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ManagedBeans;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

/**
 *
 * @author Nascotix
 */
@FacesValidator(value = "duplicateFieldValidator")
public class ValidatePasswords implements Validator {

    /**
     * Creates a new instance of ValidatePasswords
     */
    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { 
        String pwd_1 = String.valueOf(value);
        String endComponentId = (String) component.getAttributes().get("endComponentId");
        UIInput endComponent = (UIInput) context.getViewRoot().findComponent(endComponentId);
        Object pwd_2 = endComponent.getSubmittedValue();
        
        //System.err.println("Values : " + pwd_1 + " " + pwd_2);
        
        if (!pwd_1.equals(pwd_2)){
            FacesMessage message = new FacesMessage(
                    FacesMessage.SEVERITY_ERROR, "Your passwords are not the same !",
                    "Your passwords are not the same !");
            throw new ValidatorException(message);
        }
    }
}
